package com.weiadmin.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.weiadmin.service.ExcelService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能描述
 *
 * @author: shizhongyu
 * @date: 2023年06月10日 11:21
 */
@Service
public class ExcelServiceImpl implements ExcelService {
    @Value("${file.upload.path}")
    private String filePath;
    @Override
    public List<Map<String,Object>> importExcel(String path) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(path), 0);;
        List<Map<String,Object>> readAll = reader.readAll();
        return readAll;
    }

    @Override
    public String exportExcel(List<List<String>> list,String filename) {
        String filepath = filePath + IdUtil.randomUUID()+ ".xlsx";
        ExcelWriter writer = ExcelUtil.getWriter(filepath);
        //跳过当前行，既第一行，非必须，在此演示用
//        writer.passCurrentRow();
        //合并单元格后的标题行，使用默认标题样式
//        writer.merge(list.size() - 1, filename);
        //一次性写出内容，强制输出标题
        writer.renameSheet(filename);
        writer.write(list, true);
        //关闭writer，释放内存
        writer.close();
        return filepath;
    }
}
